package com.example.hzq;

import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.WorkHandler;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Disruptor事件处理器，处理业务逻辑
 */
public class MessageEventHandler implements WorkHandler<MessageEvent> {
    private static final Logger logger = LoggerFactory.getLogger(MessageEventHandler.class);
    private final int handlerId;

    public MessageEventHandler(int handlerId) {
        this.handlerId = handlerId;
    }



    @Override
    public void onEvent(MessageEvent messageEvent) throws Exception {
        try {
            // 处理业务逻辑
            String message = messageEvent.getMessage();
            ChannelHandlerContext ctx = messageEvent.getCtx();

            if (message != null && ctx != null) {
                logger.info("Handler {} processing message: {}", handlerId, message);

                // 简单的响应示例
                String response = "Processed: " + message;
                ctx.writeAndFlush(response);
            }
        } finally {
            // 清除事件数据，便于重用
            messageEvent.clear();
        }
    }
}
